package cn.wolfcode.luowowo.controller;


import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.query.TravelQuery;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("travel")
public class TravelController {


    @Autowired
    private ITravelService travelService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") TravelQuery qo){

        //看前端页面共享个page
        // pageinfo :是mybatis插件,操作的是mysql数据库
        // Page  : 是spring-data 的组件,操作的是mongodb
        Page<Travel> page =  travelService.query(qo);

        model.addAttribute("page",page);
        return "travel/list";
    }

    @RequestMapping("/input")
    public String input(Model model,String id){

        if(StringUtils.hasLength(id)){
            // 攻略strategy
            model.addAttribute("strategy",travelService.get(id));
        }


        return "strategy/input";
    }

    // 添加按钮中保存操作
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel) {
        // 保存和编辑
        travelService.saveOrUpdate(travel);
        return JsonResult.success();
    }

    // 游记管理模块：审核通过按钮
    @RequestMapping("/audit")
    @ResponseBody
    public Object audit(String id,int state) {
        // 通过id控制状态
        travelService.audit(id,state);
        return JsonResult.success();
    }
    // 游记管理模块：查看按钮（游记内容）
    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContentById(String id) {

        Travel travel = travelService.get(id);
        return JsonResult.success(travel.getContent());
    }
}
